﻿
function ExpandDIV(grdDIV, id) {
    var divID = grdDIV + id;
    var divs = document.getElementsByTagName("DIV");
    for (var i = 0; i < divs.length; i++) {
        var divObj = divs[i];
        if (divID == divObj.id) {
            if (divObj.style.display == "none" || divObj.style.display == "") {
                divObj.style.display = "block";
            }
            else {
                divObj.style.display = "none";
            }
        }
    }
}


function ExpandCollapse(e) {
    if (e != null) {
        if (e.className == "btnCollapse") {
            e.className = "btnExpand";
        }
        else {
            e.className = "btnCollapse";
        }
    }
}

function SelectDepartmentGroup(obj, lr) {
    var commonID;
    if (lr == "Left") {
        commonID = GetCommonID(obj, "chkLeftDepartmentGroup");
    }
    else if (lr == "Right") {
        commonID = GetCommonID(obj, "chkRightDepartmentGroup");
    }

    CheckDepartmentGroup(obj, commonID, null);
}

function SelectDepartment(obj, lr) {
    var commonID;
    var chkIDs = new Array();
    if (lr == "Left") {
        commonID = GetCommonID(obj, "chkLeftDepartment");
        chkIDs[0] = GetID("grdLeftDepartment_", "chkLeftDepartmentGroup", obj);
    }
    else if (lr == "Right") {
        commonID = GetCommonID(obj, "chkRightDepartment");
        chkIDs[0] = GetID("grdRightDepartment_", "chkRightDepartmentGroup", obj);
    }

    CheckDepartment(obj, commonID, chkIDs);
}

function CheckCostCenterGroup(obj, lr) {
    var commonID;
    var chkIDs = new Array();
    if (lr == "Left") {
        commonID = GetCommonID(obj, "chkLeftCostCenterGroup");
        chkIDs[0] = GetID("grdLeftCostCenterGroup_", "chkLeftDepartment", obj);
        chkIDs[1] = GetID("grdLeftDepartment_", "chkLeftDepartmentGroup", obj);
    }
    else if (lr == "Right") {
        commonID = GetCommonID(obj, "chkRightCostCenterGroup");
        chkIDs[0] = GetID("grdRightCostCenterGroup_", "chkRightDepartment", obj);
        chkIDs[1] = GetID("grdRightDepartment_", "chkRightDepartmentGroup", obj);
    }

    CheckCostCenterGroup(obj, commonID, chkIDs);
}

function SelectCostCenter(obj, lr) {
    var chkIDs = new Array();
    if (lr == "Left") {
        chkIDs[0] = GetID("grdLeftCostCenter_", "chkLeftCostCenterGroup", obj);
        chkIDs[1] = GetID("grdLeftCostCenterGroup_", "chkLeftDepartment", obj);
        chkIDs[2] = GetID("grdLeftDepartment_", "chkLeftDepartmentGroup", obj);
    }
    else if (lr == "Right") {
        chkIDs[0] = GetID("grdRightCostCenter_", "chkRightCostCenterGroup", obj);
        chkIDs[1] = GetID("grdRightCostCenterGroup_", "chkRightDepartment", obj);
        chkIDs[2] = GetID("grdRightDepartment_", "chkRightDepartmentGroup", obj);
    }

    CheckCostCenter(obj, "", chkIDs);
}

function GetCommonID(obj, chk) {
    var index = obj.id.indexOf(chk);
    return obj.id.substring(0, index - 1);
}

function GetID(grd, chk, obj) {
    var index = obj.id.indexOf(grd);
    return obj.id.substring(0, index) + chk;
}

//****************************************************************
//Vilidate the number
//****************************************************************
function VilidationText(obj) {
    this.Obj = obj;
}
VilidationText.prototype.vilidaNum = function() {
    if (this.Obj != null && this.Obj.value != "") {
        var regu = /^[-]{0,1}[0-9]{1,}$/;
        return regu.test(this.Obj.value);
    }
    return false;
}
function viliNum(obj) {
    var vilidateNo = new VilidationText(obj);
    if (!vilidateNo.vilidaNum()) {
        obj.value = "";
    }
}

//****************************************************************
//Change sub DropDownList
//****************************************************************
function ChangeDDL(obj, commonid) {
    this.ddlObj = obj;

    this.fteObjs = new Array();
    this.chkObjs = new Array();
    this.comID = commonid;
}
ChangeDDL.prototype.GetCommonID = function() {
    var index = this.ddlObj.id.indexOf(this.comID);
    return this.ddlObj.id.substring(0, index - 1);
}
ChangeDDL.prototype.GetCheckObjs = function() {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i] != undefined && inputs[i].type == "checkbox") {
            this.chkObjs.push(inputs[i]);
        }
    }
}
ChangeDDL.prototype.ChangeSelectText = function() {
    var ddls = document.getElementsByTagName("SELECT");
    for (var i = 0; i < ddls.length; i++) {
        var object = ddls[i];
        if (object != null && object.id.indexOf(this.GetCommonID()) > -1) {
            object.selectedIndex = this.ddlObj.selectedIndex;
        }
    }
}
ChangeDDL.prototype.GetFTEObjs = function() {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i] != undefined && inputs[i].type == "submit") {
            this.fteObjs.push(inputs[i]);
        }
    }
}
ChangeDDL.prototype.SetFTE = function() {
    for (var i = 0; i < this.fteObjs.length; i++) {
        var ftebutton = this.fteObjs[i];
        if (ftebutton.id.indexOf(this.GetCommonID()) > -1) {
            for (var j = 0; j < this.chkObjs.length; j++) {
                if (this.chkObjs[j].id.indexOf(this.GetCommonID()) > -1) {
                    if (this.chkObjs[j].checked && this.ddlObj.value == "3000") {
                        ftebutton.disabled = false;
                    }
                    else {
                        ftebutton.disabled = true;
                    }
                }
            }
        }
        else {
            if (ExistChkID(this.fteIDs, ftebutton.id)) {
                ftebutton.disabled = true;
            }
        }
    }
}

///Four Level
function ChangeDGAFF(obj) {
    var changeDGAFF = new ChangeDDL(obj, "ddlDGAllowBase");
    changeDGAFF.GetCheckObjs();
    changeDGAFF.GetFTEObjs();
    changeDGAFF.ChangeSelectText();
    changeDGAFF.SetFTE();
}
function ChangeDAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlDDAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}
function ChangeCCGAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlCCGAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}
function ChangeCCAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlCCAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}

///Three Levels
function ChangeSDAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlSDDAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}
function ChangeSCCGAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlSCCGAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}
function ChangeSCCAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlSCCAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}

///Two Level
function ChangeTCCGAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlTCCGAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}
function ChangeTCCAFF(obj) {
    var chageDAFF = new ChangeDDL(obj, "ddlTCCAllowBase");
    chageDAFF.GetCheckObjs();
    chageDAFF.GetFTEObjs();
    chageDAFF.ChangeSelectText();
    chageDAFF.SetFTE();
}

//****************************************************************
//CheckBox sub change for AFF page
//****************************************************************
function ExistChkID(chkboxs, id) {
    if (chkboxs != null || chkboxs != undefined) {
        for (var i = 0; i < chkboxs.length; i++) {
            if (id == chkboxs[i]) {
                return true;
            }
        }
    }
    return false;
}

function CheckObj(obj, commonid, chkArray, ddlArray, fteArray) {
    this.chkObj = obj;
    this.commID = commonid;
    this.chkIDs = chkArray;

    this.commddlID = commonid;
    this.ddlIDs = ddlArray;

    this.commFTEID = commonid;
    this.fteIDs = fteArray;

    this.checkboxObjs = new Array();
    this.dropdownlistObjs = new Array();
    this.fteObjs = new Array();
}
CheckObj.prototype.GetChkObjs = function() {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i] != undefined && inputs[i].type == "checkbox") {
            this.checkboxObjs.push(inputs[i]);
        }
    }
}
CheckObj.prototype.Checked = function() {
    for (var m = 0; m < this.checkboxObjs.length; m++) {
        var checkboxObj = this.checkboxObjs[m];
        if (this.commID != null && checkboxObj.id.indexOf(this.commID) > -1) {
            checkboxObj.checked = this.chkObj.checked;
        }
        else {
            if (ExistChkID(this.chkIDs, checkboxObj.id)) {
                checkboxObj.checked = false;
            }
        }
    }
}
CheckObj.prototype.GetDDLObjs = function() {
    this.dropdownlistObjs = document.getElementsByTagName("SELECT");
}
CheckObj.prototype.Selected = function() {
    for (var m = 0; m < this.dropdownlistObjs.length; m++) {
        var selectObj = this.dropdownlistObjs[m];
        if (this.commddlID != null && selectObj.id.indexOf(this.commddlID) > -1) {
            selectObj.disabled = !this.chkObj.checked;
        }
        else {
            if (ExistChkID(this.ddlIDs, selectObj.id)) {
                selectObj.disabled = true;
            }
        }
    }
}
CheckObj.prototype.GetFTEObjs = function() {
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i] != undefined && inputs[i].type == "submit") {
            this.fteObjs.push(inputs[i]);
        }
    }
}
CheckObj.prototype.SetFTE = function() {
    for (var m = 0; m < this.fteObjs.length; m++) {
        var ftebutton = this.fteObjs[m];
        if (this.commFTEID != null && ftebutton.id.indexOf(this.commFTEID) > -1) {
            for (var i = 0; i < this.dropdownlistObjs.length; i++) {
                if (this.dropdownlistObjs[i].id.indexOf(this.commID) > -1) {
                    if (this.dropdownlistObjs[i].value == "3000") {
                        ftebutton.disabled = !this.chkObj.checked;
                        break;
                    }
                }
            }
        }
        else {
            if (ExistChkID(this.fteIDs, ftebutton.id)) {
                ftebutton.disabled = true;
            }
        }
    }
}
CheckObj.prototype.SetFTEOnValue = function(chk, ddlId) {
    var chkSelect = chk.checked;
}

///Four Levels
function SelectDepartmentGroupAFF(obj) {
    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("chkDGAFFFlag", "ddlDGAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("chkDGAFFFlag", "btnDepartmentGroupFTEEdit", obj);

    var selectDGAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkDGAFFFlag"), null, ddlIDs, fteButtons);
    selectDGAFF.GetChkObjs();
    selectDGAFF.Checked();
    selectDGAFF.GetDDLObjs();
    selectDGAFF.Selected();
    selectDGAFF.GetFTEObjs();
    selectDGAFF.SetFTE();
}
function SelectDepartmentAFF(obj) {
    var chkIDs = new Array();
    chkIDs[0] = OrgnizeID("grdDepartment_", "chkDGAFFFlag", obj);

    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("grdDepartment_", "ddlDGAllowBase", obj);
    ddlIDs[1] = OrgnizeID("chkDAFFFlag", "ddlDDAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("grdDepartment_", "btnDepartmentGroupFTEEdit", obj);
    fteButtons[1] = OrgnizeID("chkDAFFFlag", "btnDepartmentFTEEdit", obj);

    var selectDAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkDAFFFlag"), chkIDs, ddlIDs, fteButtons);
    selectDAFF.GetChkObjs();
    selectDAFF.Checked();
    selectDAFF.GetDDLObjs();
    selectDAFF.Selected();
    selectDAFF.GetFTEObjs();
    selectDAFF.SetFTE();
}
function SelectCostCenterGroupAFF(obj) {
    var chkIDs = new Array();
    chkIDs[0] = OrgnizeID("grdDepartment_", "chkDGAFFFlag", obj);
    chkIDs[1] = OrgnizeID("grdCostCenterGroup_", "chkDAFFFlag", obj);

    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("grdDepartment_", "ddlDGAllowBase", obj);
    ddlIDs[1] = OrgnizeID("grdCostCenterGroup_", "ddlDDAllowBase", obj);
    ddlIDs[2] = OrgnizeID("chkCostCCGAFFFlag", "ddlCCGAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("grdDepartment_", "btnDepartmentGroupFTEEdit", obj);
    fteButtons[1] = OrgnizeID("grdCostCenterGroup_", "btnDepartmentFTEEdit", obj);
    fteButtons[2] = OrgnizeID("chkCostCCGAFFFlag", "btnCostCenterGroupCodeFTEEdit", obj);

    var selectCCGAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkCostCCGAFFFlag"), chkIDs, ddlIDs, fteButtons);
    selectCCGAFF.GetChkObjs();
    selectCCGAFF.Checked();
    selectCCGAFF.GetDDLObjs();
    selectCCGAFF.Selected();
    selectCCGAFF.GetFTEObjs();
    selectCCGAFF.SetFTE();
}
function SelectCostCenterAFF(obj) {
    var chkIDs = new Array();
    chkIDs[0] = OrgnizeID("grdDepartment_", "chkDGAFFFlag", obj);
    chkIDs[1] = OrgnizeID("grdCostCenterGroup_", "chkDAFFFlag", obj);
    chkIDs[2] = OrgnizeID("grdCostCenter_", "chkCostCCGAFFFlag", obj);

    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("grdDepartment_", "ddlDGAllowBase", obj);
    ddlIDs[1] = OrgnizeID("grdCostCenterGroup_", "ddlDDAllowBase", obj);
    ddlIDs[2] = OrgnizeID("grdCostCenter_", "ddlCCGAllowBase", obj);
    ddlIDs[3] = OrgnizeID("chkCCAFFFlag", "ddlCCAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("grdDepartment_", "btnDepartmentGroupFTEEdit", obj);
    fteButtons[1] = OrgnizeID("grdCostCenterGroup_", "btnDepartmentFTEEdit", obj);
    fteButtons[2] = OrgnizeID("grdCostCenter_", "btnCostCenterGroupCodeFTEEdit", obj);
    fteButtons[3] = OrgnizeID("chkCCAFFFlag", "btnCostCenterFTEEdit", obj);

    var selectCCAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkCCAFFFlag"), chkIDs, ddlIDs, fteButtons);
    selectCCAFF.GetChkObjs();
    selectCCAFF.Checked();
    selectCCAFF.GetDDLObjs();
    selectCCAFF.Selected();
    selectCCAFF.GetFTEObjs();
    selectCCAFF.SetFTE();
}

///Three Levels
function SelectSDepartmentAFF(obj) {
    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("chkSDAFFFlag", "ddlSDDAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("chkSDAFFFlag", "btnSDepartmentFTEEdit", obj);

    var selectDGAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkSDAFFFlag"), null, ddlIDs, fteButtons);
    selectDGAFF.GetChkObjs();
    selectDGAFF.Checked();
    selectDGAFF.GetDDLObjs();
    selectDGAFF.Selected();
    selectDGAFF.GetFTEObjs();
    selectDGAFF.SetFTE();
}
function SelectSCostCenterGroupAFF(obj) {
    var chkIDs = new Array();
    chkIDs[0] = OrgnizeID("grdSCostCenterGroup_", "chkSDAFFFlag", obj);

    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("grdSCostCenterGroup_", "ddlSDDAllowBase", obj);
    ddlIDs[1] = OrgnizeID("chkSCostCCGAFFFlag", "ddlSCCGAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("grdSCostCenterGroup_", "btnSDepartmentFTEEdit", obj);
    fteButtons[1] = OrgnizeID("chkSCostCCGAFFFlag", "btnSCostCenterGroupCodeFTEEdit", obj);

    var selectDAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkSCostCCGAFFFlag"), chkIDs, ddlIDs, fteButtons);
    selectDAFF.GetChkObjs();
    selectDAFF.Checked();
    selectDAFF.GetDDLObjs();
    selectDAFF.Selected();
    selectDAFF.GetFTEObjs();
    selectDAFF.SetFTE();
}
function SelectSCostCenterAFF(obj) {
    var chkIDs = new Array();
    chkIDs[0] = OrgnizeID("grdSCostCenterGroup_", "chkSDAFFFlag", obj);
    chkIDs[1] = OrgnizeID("grdSCostCenter_", "chkSCostCCGAFFFlag", obj);

    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("grdSCostCenterGroup_", "ddlSDDAllowBase", obj);
    ddlIDs[1] = OrgnizeID("grdSCostCenter_", "ddlSCCGAllowBase", obj);
    ddlIDs[2] = OrgnizeID("chkSCCAFFFlag", "ddlSCCAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("grdSCostCenterGroup_", "btnSDepartmentFTEEdit", obj);
    fteButtons[1] = OrgnizeID("grdSCostCenter_", "btnSCostCenterGroupCodeFTEEdit", obj);
    fteButtons[2] = OrgnizeID("chkSCCAFFFlag", "btnSCostCenterFTEEdit", obj);

    var selectCCGAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkSCCAFFFlag"), chkIDs, ddlIDs, fteButtons);
    selectCCGAFF.GetChkObjs();
    selectCCGAFF.Checked();
    selectCCGAFF.GetDDLObjs();
    selectCCGAFF.Selected();
    selectCCGAFF.GetFTEObjs();
    selectCCGAFF.SetFTE();
}
///Two Levels
function SelectTCostCenterGroupAFF(obj) {
    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("chkTCostCCGAFFFlag", "ddlTCCGAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("chkTCostCCGAFFFlag", "btnTCostCenterGroupCodeFTEEdit", obj);

    var selectDAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkTCostCCGAFFFlag"), null, ddlIDs, fteButtons);
    selectDAFF.GetChkObjs();
    selectDAFF.Checked();
    selectDAFF.GetDDLObjs();
    selectDAFF.Selected();
    selectDAFF.GetFTEObjs();
    selectDAFF.SetFTE();
}
function SelectTCostCenterAFF(obj) {
    var chkIDs = new Array();
    chkIDs[0] = OrgnizeID("grdTCostCenter_", "chkTCostCCGAFFFlag", obj);

    var ddlIDs = new Array();
    ddlIDs[0] = OrgnizeID("grdTCostCenter_", "ddlTCCGAllowBase", obj);
    ddlIDs[1] = OrgnizeID("chkTCCAFFFlag", "ddlTCCAllowBase", obj);

    var fteButtons = new Array();
    fteButtons[0] = OrgnizeID("grdTCostCenter_", "btnTCostCenterGroupCodeFTEEdit", obj);
    fteButtons[1] = OrgnizeID("chkTCCAFFFlag", "btnTCostCenterFTEEdit", obj);

    var selectCCGAFF = new CheckObj(obj, OrgnizeCommonID(obj, "_chkTCCAFFFlag"), chkIDs, ddlIDs, fteButtons);
    selectCCGAFF.GetChkObjs();
    selectCCGAFF.Checked();
    selectCCGAFF.GetDDLObjs();
    selectCCGAFF.Selected();
    selectCCGAFF.GetFTEObjs();
    selectCCGAFF.SetFTE();
}

///One Levels




//****************************************************************
//Common functions
//****************************************************************
function OrgnizeCommonID(obj, chkid) {
    var index = obj.id.indexOf(chkid);
    return obj.id.substring(0, index);
}
function OrgnizeID(grd, chk, obj) {
    var index = obj.id.indexOf(grd);
    return obj.id.substring(0, index) + chk;
}
function VilidateNum(obj) {
    var str = /^-?\d+$/;
    if (!str.test(obj.value)) {
        obj.value = "";
    }
}



